
<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
	session_start();
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="js/ajaxInit.js"></script>
        <link rel="stylesheet" type="text/css" href="css/style.css"/>
        <script type="text/javascript">
			function atualizaCampos(){	
                var formulario = document.getElementById('formulario');
                formulario.submit();
			}
			
            function concluirAluguel(){
                ajax = ajaxInit();
                var idaluguel = document.getElementById("idaluguel2").value;
                var formulario = document.getElementById("formulario");
                if(ajax){
                    ajax.onreadystatechange = function()
                    {
                        if (ajax.readyState==4 && ajax.status==200)
                        {
                            if(ajax.responseText == 'sucesso'){
                                alert('Aluguel pago com sucesso!');
                                formulario.submit();
                            } else {
                                alert('Falha ao realizar pagamento de aluguel.');
                            }
                        }
                    }
                    ajax.open("GET","http://localhost/caralugue/concluirPagamentoAluguel.php?idaluguel	="+idaluguel+"",true);
                    ajax.send();
                }
            }
            
        </script>
    </head>
    <body>
		<div id="usuario" align="right">
            <?php
            if (isset($_SESSION['login'])) {
                echo "Bem vindo " . $_SESSION['login'] . ".<br />";
                echo "<a href=\"index.php\">sair</a>";
            } else {
                echo "<span style=\"color: red; font-weight: bold;\">Você não é um usuário cadastrado.</span>";
            }
            ?>
        </div>
        <form id="formulario" action="" method="POST">
        <div id="painel">
			<div id="subpainel">
            <span class="destaque">CARALUGUE - Formulário de Pagamento de Aluguel</span>
            <br />
            <span class="pequeno">Pagamento de aluguel do sistema.</span>
            <br /><br />
            <div class="formulario">
				<label class="destaque">ALUGUEL</label><br />
				<label class="pequeno, cinza">Selecione o aluguel que deseja pagar.</label><br />
				<select id="idaluguel" name="idaluguel" onchange="atualizaCampos()">
					<option value="-1">Selecione o aluguel</option>
					<?php
						include_once("conexao.php");
						include_once("utils.php");
						$query = "SELECT * FROM aluguel WHERE pg = 1";
						$sql = mysql_query($query) or die(mysql_error());
						while ($res = mysql_fetch_array($sql)) {
							$res2 = mysql_fetch_array(mysql_query("SELECT * FROM cliente WHERE idcliente=".$res['cliente'])) or die(mysql_error()); 
							$res3 = mysql_fetch_array(mysql_query("SELECT * FROM veiculo WHERE idveiculo=".$res['veiculo'])) or die(mysql_error()); 
							echo "<option value='". $res['idaluguel'] ."'>". databr($res['data_aluguel']) . " - " 
							. $res3['placa'] . " - ". $res3['modelo']. " - " . $res2['nome'] . "</option>";
						}
					?>
				</select><br /><br />
					<?php
						include_once("conexao.php");
						if($_POST['idaluguel'] == '-1' || !(isset($_POST['idaluguel']))){
							$res = "";
							$res2 = "";
							$res3 = "";
						} else {
							$query = "SELECT * FROM aluguel WHERE idaluguel = ".$_POST['idaluguel'];
							$sql = mysql_query($query) or die(mysql_error());
							$res = mysql_fetch_array($sql); 
							$res2 = mysql_fetch_array(mysql_query("SELECT * FROM cliente WHERE idcliente=".$res['cliente'])) or die(mysql_error()); 
							$res3 = mysql_fetch_array(mysql_query("SELECT * FROM veiculo WHERE idveiculo=".$res['veiculo'])) or die(mysql_error()); 
						}
					?>
					<br /><br />
					<input type="hidden" id="idaluguel2" name="idaluguel2" value="<?php echo $res['idaluguel']; ?>" />
					<label class="destaque">ALUGUEL A SER CONCLUIDO:</label>
					<label id="valor" class="pequeno, cinza"><?php echo databr($res['data_aluguel']) . " - " 
							. $res3['placa'] . " - ". $res3['modelo']. " - " . $res2['nome']; ?></label><br />
					<label class="destaque">VALOR A PAGAR: R$</label>
					<label id="valor" class="destaque"><?php echo $res['total']; ?></label>
					<br /><br />
				</div><br>
				<a href="home.php"><input type="button" name="voltar" value="Voltar"></a>
				<input type="button" value="Pagar" onclick="if(confirm('Deseja realmente realizar o pagamento do aluguel?'))
					concluirAluguel();"/>
            </div>
		</form>
	</body>
</html>
